package com.aabbc.elasticsearch.controller;

import com.aabbc.elasticsearch.document.OrderDocument;
import com.aabbc.elasticsearch.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * OrderController
 *
 * @author: <a href="mailto:chenfenghai@oristartech.com">chenfenghai</a>
 * @version: 1.0
 * @date: 2019-11-26 14:24
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;

    @GetMapping("/test")
    public String test(){
        return "IndexController.test";
    }

    @PostMapping
    public String createIndex(){
        boolean exists = elasticsearchTemplate.indexExists(OrderDocument.class);
        if(!exists){
            elasticsearchTemplate.createIndex(OrderDocument.class);
        }
        return "success";
    }

    @DeleteMapping
    public String deleteIndex(){
        boolean exists = elasticsearchTemplate.indexExists(OrderDocument.class);
        if(exists){
            elasticsearchTemplate.deleteIndex(OrderDocument.class);
        }
        return "success";
    }
}
